package com.example.usb.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.example.usb.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * created by liangkun on ${DATA}
 * Describe:人脸适配器
 */

public class FaceAllAdapter extends RecyclerView.Adapter<FaceAllAdapter.ViewHolder> {
    private Context context;
    private List<String> faceList;

    public FaceAllAdapter(Context context, List<String> faceList) {
        this.context = context;
        this.faceList = faceList;
    }

    @NonNull
    @Override
    public FaceAllAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_image, viewGroup, false);
        return new FaceAllAdapter.ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull FaceAllAdapter.ViewHolder viewHolder, int i) {
        if (faceList != null) {
            String path = faceList.get(i);
            if (!TextUtils.isEmpty(path)) {
                Bitmap bm = BitmapFactory.decodeFile(path);
                viewHolder.imageView.setImageBitmap(bm);
            }

        }

    }
    public void setData(List<String> headList) {
        faceList.clear();
        faceList = headList;
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        if (faceList != null) {
            return faceList.size();
        }
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_item_head)
        ImageView imageView;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
